package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Creat with IntelliJ IDEA
 * Description:
 * User:WZW
 * Date:2022-08-17
 * Time:17:30
 */
//@RestController
@Controller
public class TestController {
    @ResponseBody
    @RequestMapping("/sayhi")
    public String sayhi() {
        return "450";
    }

    // 请求转发实现方式1
    @RequestMapping("/fw")
    public String myForward() {
//        return "forward:/hello.html";
        return "/hello.html";//默认返回一个静态页面，就是请求转发的过程
    }

    // 请求转发实现方式2
    @RequestMapping("/fw2")
    public void myForward2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/hello.html").forward(request,response);
    }

    //请求重定向实现1
    @RequestMapping("/rd")
    public String myRedirect() {
        return "redirect:/hello.html";
    }

    //请求重定向实现2
    @RequestMapping("/rd2")
    public void myRedirect2(HttpServletResponse response) throws IOException {
        response.sendRedirect("/hello.html");
    }

}
